package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.entity.Tourism;
import com.example.service.TourismService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("tourisms")
public class TourismController {

    @Autowired
    private TourismService tourismService;

    /**
     * 旅游攻略分页查询
     * @return
     */
    @GetMapping("/getPageTourism/{currentPage}/{pageSize}")
    public R getPage(@PathVariable int currentPage,@PathVariable int pageSize,Tourism tourism){

        IPage page=new Page(currentPage,pageSize);
        LambdaQueryWrapper<Tourism> lqw =new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(tourism.getTitle()),Tourism::getTitle,tourism.getTitle());

        IPage page1 = tourismService.page(page,lqw);


        return R.success("分页查询所有tourisms成功！",page1);
    }


    /**
     * 旅游攻略列表查询
     * @return
     */
    @GetMapping("/getListTourism")
    public R getListTourism(){

        List<Tourism> tourismList = tourismService.list();
        return R.success("旅游攻略列表查询！",tourismList);
    }

    /**
     * 旅游攻略列表查询(条件)
     * @return
     */
    @GetMapping("/getListTourismByContent")
    public R getListTourismByContent(@RequestParam String content2) {
        LambdaQueryWrapper<Tourism> lqw = new LambdaQueryWrapper<>();

        // 使用 or 方法将两个条件视为 "或" 关系
        lqw.and(wrapper -> wrapper
                .like(Strings.isNotEmpty(content2), Tourism::getTitle, content2)
                .or()
                .like(Strings.isNotEmpty(content2), Tourism::getDescription, content2));

        List<Tourism> tourismList = tourismService.list(lqw);
        return R.success("旅游攻略搜索查询！", tourismList);
    }


    /**
     * 添加旅游攻略
     * @return
     */
    @PostMapping("/addTourism")
    public R addTourism(@RequestBody Tourism tourism){

        boolean save = tourismService.save(tourism);
        if (save){
            return R.success("添加旅游攻略成功！",null);
        }else {
            return R.error("添加旅游攻略失败！");
        }
    }

    /**
     * 通过id查询旅游攻略
     * @return
     */
    @GetMapping("/getTourismById")
    public R getTourismById(@RequestParam int id) {

        LambdaQueryWrapper<Tourism> lqw =new LambdaQueryWrapper<>();
        lqw.eq(Tourism::getId,id);
        Tourism one = tourismService.getOne(lqw);
        return R.success("查询旅游攻略",one);
    }

    /**
     * 修改旅游攻略
     * @return
     */
    @PutMapping("/updateTourism")
    public R updateTourism(@RequestBody Tourism tourism){
        boolean b = tourismService.updateById(tourism);
        if (b){
           return R.success("修改旅游攻略成功",tourism);
        }else {
           return R.error("修改旅游攻略失败！");
        }
    }

    /**
     * 通过id删除旅游攻略
     * @return
     */
    @DeleteMapping("/deleteTourism/{id}")
    public R deleteTourism(@PathVariable int id){

        boolean b = tourismService.removeById(id);
        if (b){
           return R.success("删除旅游攻略成功",id);
        }else {
           return R.error("删除旅游攻略失败！");
        }
    }



}

